//引入供应商数据模型
const { supplierModel } = require('../modules/supplierModule');

//获取数据库供应商
async function getSupplier(req, res, next) {
    const { value, currentPage, pageSize } = req.query;
    //第一步先获取数据库总数据
    const total = await supplierModel.countDocuments({
        $or: [
            { supplierName: { $regex: value, $options: '$i' } },
            { supplierMan: { $regex: value, $options: '$i' } },
            { supplierTel: { $regex: value, $options: '$i' } },
            { supplierEmail: { $regex: value, $options: '$i' } },
            { supplierAddress: { $regex: value, $options: '$i' } },
            { supplierNote: { $regex: value, $options: '$i' } },
        ]
    });
    //换算成对应的页数
    const pages = Math.ceil(total / pageSize);

    const result = await supplierModel
        //模糊查询用户输入
        .find({
            $or: [
                { supplierName: { $regex: value, $options: '$i' } },
                { supplierMan: { $regex: value, $options: '$i' } },
                { supplierTel: { $regex: value, $options: '$i' } },
                { supplierEmail: { $regex: value, $options: '$i' } },
                { supplierAddress: { $regex: value, $options: '$i' } },
                { supplierNote: { $regex: value, $options: '$i' } },
            ]
        })
        .limit(pageSize - 0)//一页显示几条,减零是为了转换为number
        .skip((currentPage - 1) * pageSize)//跳过多少条数据展示

    if (result.length > 0) {
        res.send({
            message: "供应商获取成功",
            code: 200,
            result,
            total,
            pages
        })
    } else {
        res.send({
            message: "供应商数据为空",
            code: 500
        })
    }
}

//添加供应商
async function addSupplier(req, res, next) {
    const data = req.query;
    const result = await supplierModel.create(data);
    if (result) {
        res.send({
            message: "添加成功",
            code: 200
        })
    } else {
        res.send({
            message: "添加失败",
            code: 500
        })
    }

}
//删除供应商
async function delSupplier(req, res, next) {
    const data = req.body;
    const result = await supplierModel.deleteOne(data);
    if (result.deletedCount > 0) {
        res.send({
            message: "删除成功",
            code: 200
        })
    } else {
        res.send({
            message: "删除失败",
            code: 500
        })
    }
}
//查找供应商
async function findSupplier(req, res, next) {
    const data = req.query;

    const result = await supplierModel.find(data);
    if (result) {
        res.send({
            message: "获取成功",
            code: 200,
            result
        })
    } else {
        res.send({
            message: "获取失败",
            code: 500
        })
    }
}
//修改供应商
async function changeSupplier(req, res, next) {
    const data = req.body;
    const result = await supplierModel.updateOne({ _id: data._id }, data);
    if (result.nModified > 0) {
        res.send({
            message: "修改成功",
            code: 200
        })
    } else {
        res.send({
            message: "修改失败",
            code: 500
        })
    }
}
//获取总共的供应商
async function totalSupplier(req, res, next) {
    const { value, currentPage, pageSize } = req.body;
    //第一步先获取数据库总数据
    const total = await supplierModel.countDocuments({
        $or: [
            { supplierName: { $regex: value, $options: '$i' } },
            { supplierMan: { $regex: value, $options: '$i' } },
            { supplierTel: { $regex: value, $options: '$i' } },
            { supplierEmail: { $regex: value, $options: '$i' } },
            { supplierAddress: { $regex: value, $options: '$i' } },
            { supplierNote: { $regex: value, $options: '$i' } },
        ]
    });
    //换算成对应的页数
    const pages = Math.ceil(total / pageSize);
    if(pages>=1){
        res.send({
            message:"总页数换算成功",
            code:200,
            pages
        })
    }else{
        res.send({
            message:"总页数换算失败",
            code:500,
            pages:1
        })  
    }
}

module.exports = {
    addSupplier, getSupplier, delSupplier, findSupplier, changeSupplier, totalSupplier
}